﻿package tabinactivity.eastandroid.com;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Log {
    static String tag = new String();
    static String msg = new String();
    static boolean release = false;

    public static void l2(Object... args) {
        if (release)
            return;
        Exception e = new Exception();
        getString2(e);
        android.util.Log.e(tag, "f :" + getArgs(args) + msg);
        getString(e);
        android.util.Log.e(tag, "f :" + getArgs(args) + msg);
    }

    public static void da(long milliseconds) {

        if (release)
            return;
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        String args = format.format(new Date(milliseconds));

        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "f :" + getArgs(new Object[] { args }) + msg);
    }

    public static void e(Object... args) {
        if (release)
            return;
        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "!!:" + getArgs(args) + msg);
        e.printStackTrace();
    }

    public static void w(Object... args) {
        if (release)
            return;
        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "! :" + getArgs(args) + msg);
        e.printStackTrace();
    }

    public static void l() {
        if (release)
            return;
        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "f :" + msg);
    }

    public static void l(Object... args) {
        if (release)
            return;
        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "f :" + getArgs(args) + msg);
    }

    public static void c(Object... args) {
        if (release)
            return;
        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "? :" + getArgs(args) + msg);
        e.printStackTrace();
    }

    public static void d(Object... args) {
        if (release)
            return;
        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "d :" + getArgs(args) + msg);
    }

    public static void v(Object... args) {
        if (release)
            return;
        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "v :" + getArgs(args) + msg);
    }

    public static void i(Object... args) {
        if (release)
            return;
        Exception e = new Exception();
        getString(e);
        android.util.Log.e(tag, "i :" + getArgs(args) + msg);
    }

    private static String getArgs(final Object[] args) {
        StringBuffer sb = new StringBuffer();
        for (Object object : args) {
            sb.append(",");
            if (object == null)
                object = "null";
            sb.append(object.toString());
        }
        return sb.toString();
    }

    private static void getString(final Exception e) {
        String funcStack = e.getStackTrace()[1].toString();

        int posJava = funcStack.lastIndexOf("(");
        int posFunc = funcStack.lastIndexOf(".", posJava - 1);
        int posClass = funcStack.lastIndexOf(".", posFunc - 1);
        tag = funcStack.substring(posClass + 1, posJava);// classfuncName
        msg = ":at " + funcStack.substring(posJava);// javaName
    }

    private static void getString2(final Exception e) {
        String funcStack = e.getStackTrace()[2].toString();

        int posJava = funcStack.lastIndexOf("(");
        int posFunc = funcStack.lastIndexOf(".", posJava - 1);
        int posClass = funcStack.lastIndexOf(".", posFunc - 1);
        tag = funcStack.substring(posClass + 1, posJava);// classfuncName
        msg = ":at " + funcStack.substring(posJava);// javaName
    }

}
